package com.crayon2f.test.xml;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.imageio.ImageIO;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * Created by feifan.gou@gmail.com on 2025/4/10 10:12.
 */
public class GenerateGraphic {

    @Test
    public void export() {

        try {
            // 1. 加载XML文件
            File xmlFile = new File("D:\\download\\temp.xml");

            // 2. 解析XML并生成图片
            BufferedImage image = createImageFromXml(xmlFile);

            // 3. 保存图片
            File output = new File("D:\\download\\shape_export.png");
            ImageIO.write(image, "png", output);

            System.out.println("图片已成功导出到: " + output.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static BufferedImage createImageFromXml(File xmlFile) throws Exception {
        // 解析XML
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);

        Element shape = doc.getDocumentElement();

        // 获取尺寸
        NodeList sizeNodes = shape.getElementsByTagName("size");
        int width = 200, height = 100; // 默认值
        if (sizeNodes.getLength() > 0) {
            Element size = (Element) sizeNodes.item(0);
            width = Integer.parseInt(size.getAttribute("width"));
            height = Integer.parseInt(size.getAttribute("height"));
        }

        // 创建图像
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 绘制形状
        drawShape(g2d, shape, width, height);

        g2d.dispose();
        return image;
    }

    private static void drawShape(Graphics2D g2d, Element shape, int width, int height) {
        String type = shape.getAttribute("type");

        // 处理填充色
        NodeList solidNodes = shape.getElementsByTagName("solid");
        Color fillColor = Color.GRAY; // 默认填充色
        if (solidNodes.getLength() > 0) {
            Element solid = (Element) solidNodes.item(0);
            fillColor = Color.decode(solid.getAttribute("color"));
        }

        // 处理圆角
        int cornerRadius = 0;
        NodeList cornersNodes = shape.getElementsByTagName("corners");
        if (cornersNodes.getLength() > 0) {
            Element corners = (Element) cornersNodes.item(0);
            cornerRadius = Integer.parseInt(corners.getAttribute("radius"));
        }

        // 创建形状对象
        Shape shapeObj;
        switch (type) {
            case "rectangle":
                if (cornerRadius > 0) {
                    shapeObj = new RoundRectangle2D.Float(0, 0, width, height,
                            cornerRadius, cornerRadius);
                } else {
                    shapeObj = new Rectangle2D.Float(0, 0, width, height);
                }
                break;
            case "oval":
                shapeObj = new Ellipse2D.Float(0, 0, width, height);
                break;
            default:
                throw new IllegalArgumentException("未知的形状类型: " + type);
        }

        // 绘制填充
        g2d.setColor(fillColor);
        g2d.fill(shapeObj);

        // 处理描边
        NodeList strokeNodes = shape.getElementsByTagName("stroke");
        if (strokeNodes.getLength() > 0) {
            Element stroke = (Element) strokeNodes.item(0);
            int strokeWidth = Integer.parseInt(stroke.getAttribute("width"));
            Color strokeColor = Color.decode(stroke.getAttribute("color"));

            g2d.setStroke(new BasicStroke(strokeWidth));
            g2d.setColor(strokeColor);
            g2d.draw(shapeObj);
        }

        // 处理padding（这里只是示例，实际需要调整绘制区域）
        NodeList paddingNodes = shape.getElementsByTagName("padding");
        if (paddingNodes.getLength() > 0) {
            Element padding = (Element) paddingNodes.item(0);
            // 可以在这里实现padding逻辑
        }
    }
}
